package it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyUtils;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;


public class FLightBox implements PopupListener {
    private Image png;
    private PopupPanel child;
    private PopupPanel background;
    private WindowResizeListener windowResizeListener;


    public FLightBox(PopupPanel child) {
        background = new PopupPanel();

        windowResizeListener = new WindowResizeListener() {
            public void onWindowResized(int width, int height) {
                background.setWidth(Integer.toString(width));
                background.setHeight(Integer.toString(height));
                png.setPixelSize(width, height);
                background.setPopupPosition(0, 0);
            }
        };
        Window.addWindowResizeListener(windowResizeListener);

        this.child = child;
        this.child.addPopupListener(this);
    }


    private native void backgroundFixup(Element e)
        /*-{
            if (e.__frame) {
                e.__frame.style.visibility = 'hidden';
            }
        }-*/;


    public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
        if (png != null) {
            this.hide();
        }
    }


    public void show() {
        int w = getWidth();
        int h = getHeight();

        background.setWidth(Integer.toString(w));
        background.setHeight(Integer.toString(h));
        background.setWidget(png = new Image("fuzzyWidgetsImages/lightbox.png"));
        background.setPopupPosition(0, 0);
        hideSelects();

        background.show();
        backgroundFixup(background.getElement());

        child.show();
        center();
    }


    private void center() {
        int left = (getWidth() - child.getOffsetWidth()) / 2;
        int top = (getHeight() - child.getOffsetHeight()) / 2;
        child.setPopupPosition(left, top);
    }


    public void hide() {
        png.removeFromParent();
        png = null;
        showSelects();
        child.hide();
        background.hide();
        Window.removeWindowResizeListener(windowResizeListener);
    }


    private native void hideSelects() /*-{
        var selects = $doc.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "hidden";
        }
    }-*/;


    private native void showSelects() /*-{
        var selects = $doc.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "visible";
        }
    }-*/;


    private native int getHeight() /*-{
        var yScroll;

        if ($wnd.innerHeight && $wnd.scrollMaxY) {
            yScroll = $wnd.innerHeight + $wnd.scrollMaxY;
        }
        else if ($doc.body.scrollHeight > $doc.body.offsetHeight){
            yScroll = $doc.body.scrollHeight;
        }
        else {
        yScroll = $doc.body.offsetHeight;
        }

        var windowHeight;
        if (self.innerHeight) {
            windowHeight = self.innerHeight;
        }
        else if ($doc.documentElement && $doc.documentElement.clientHeight) {
            windowHeight = $doc.documentElement.clientHeight;
        }
        else if ($doc.body) {
            windowHeight = $doc.body.clientHeight;
        }


        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        }
        else {
            pageHeight = yScroll;
        }
        return pageHeight;
    }-*/;


    private native int getWidth() /*-{
        var xScroll;

        if ($wnd.innerHeight && $wnd.scrollMaxY) {
            xScroll = $doc.body.scrollWidth;
        }
        else if ($doc.body.scrollHeight > $doc.body.offsetHeight){
            xScroll = $doc.body.scrollWidth;
        }
        else {
            xScroll = $doc.body.offsetWidth;
        }

        var windowHeight;
        if (self.innerHeight) {
            windowWidth = self.innerWidth;
        }
        else if ($doc.documentElement && $doc.documentElement.clientHeight) {
            windowWidth = $doc.documentElement.clientWidth;
        }
        else if ($doc.body) {
            windowWidth = $doc.body.clientWidth;
        }

        if(xScroll < windowWidth){	
            pageWidth = windowWidth;
        }
        else {
            pageWidth = xScroll;
        }
        return pageWidth;
    }-*/;

}
